﻿using System.ComponentModel;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class EDITAPZUORD : UserControl
    { 
        CollectionViewSource vwAPZUORD
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwAPZUORD");
            }
        }
        DataRow dwAPZUORD
        {
            get
            {
                if (vwAPZUORD.View == null)
                    return null;
                if ((DataRowView)vwAPZUORD.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwAPZUORD.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwAPKOPF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwAPKOPF");
            }
        }
        DataRow dwAPKOPF
        {
            get
            {
                if (vwAPKOPF.View == null)
                    return null;
                if ((DataRowView)vwAPKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwAPKOPF.View.CurrentItem).Row;
            }
        }

        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        bool IsLoaded = false;
        BindingListCollectionView cvAPK
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgAPK.ItemsSource);
            }
        }
        SETINTEXDB SetMaster
        {
            get
            {
                return MainViewModel.SetMaster;
            }
        }
        SETINTEXDB SetEnv
        {
            get
            {
                return MainViewModel.SetEnv;
            }
        }
        SETINTEXDB SetDetail
        {
            get
            {
                return MainViewModel.SetDetail;
            }
        }
        MainViewModel MainViewModel;
        public EDITAPZUORD()
        {
       
            InitializeComponent();
            this.Loaded += EditPlanRules_Loaded;
        }

        void EditPlanRules_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            IsLoaded = true;
            MainViewModel = (MainViewModel)this.DataContext;
            if (vwAPZUORD.View != null)
            {
                vwAPZUORD.View.CurrentChanged += vwAPZUORD_CurrentChanged;
            }
        }
        
        void vwAPZUORD_CurrentChanged(object sender, EventArgs e)
        {
            if (dwAPZUORD == null)
                return;
            string APZSTUFE = dwAPZUORD["APZSTUFE"].ToString();
            string filter = "APKSTUFE='" + APZSTUFE + "'";
            cvAPK.CustomFilter = filter;
        } 
        private void btnAddAPZ_Click(object sender, RoutedEventArgs e)
        {
            if (dwAPKOPF == null)
            {
                MessageBox.Show("请选择一个流程");
                return;
            }
            if (dwAPZUORD == null)
            {
                MessageBox.Show("请选择一个物料");
                return;
            }
            dwAPZUORD["APZANRKRS"] = dwAPKOPF["APKANRKRS"];
            dwAPZUORD["APZALFDNR"] = dwAPKOPF["APKALFDNR"];
            INTEXDBC.SaveAPZUORD(dwAPZUORD, null);
        } 
    }
}
